var user = getUserInfo();
var vue = new Vue({
    el: "#app",
    data: {
        strategy: {},
        page: {},
        vo: {},
        sids: []
    },
    methods: {
        strategyThumbup: function () {
            ajaxPost("/strategies/strategyThumbup", {sid: vue.strategy.id}, function (data) {
                if (data.data) {
                    popup("顶成功啦");
                } else {
                    popup("今天你已经顶过了");
                }
                vue.queryStatisVo(vue.strategy.id);
            })
        },
        favor: function () {
            ajaxPost("/strategies/favor", {sid: vue.strategy.id}, function (data) {
                if (data.data) {
                    popup("收藏成功");
                } else {
                    popup("已取消收藏");
                }
                vue.queryStatisVo(vue.strategy.id);
                if (user) {
                    vue.queryUserFavor(vue.strategy.id, user.id);
                }

            })
        },
        contentFocus: function () {
            $("#content").focus();
        },
        /*点赞*/
        commentThumb: function (commentId) {
            var page = $("#pagination").find("a.active").html() || 1;
            ajaxPost("/strategies/commentThumb", {cid: commentId, sid: getParams().id}, function (data) {
                vue.commentPage(page, getParams().id);
            })
        },
        mouseover: function (even) {
            $(even.currentTarget).find(".rep-del").css("display", "block");
        },
        mouseout: function (even) {
            $(even.currentTarget).find(".rep-del").css("display", "none");
        },
        commentPage: function (page, strategyId) {//分页
            strategyId = strategyId || vue.strategy.id;
            ajaxGet("/strategies/comments", {currentPage: page, strategyId: strategyId}, function (data) {
                vue.page = data.data;
                buildPage(vue.page.number, vue.page.totalPages, vue.commentPage);
            })
        },
        addComment: function () { //添加评论
            var param = {}
            param.strategyId = vue.strategy.id;
            param.strategyTitle = vue.strategy.title;

            var content = $("#content").val();
            if (!content) {
                popup("评论内容必填");
                return;
            }
            param.content = content;
            $("#content").val('');

            ajaxPost("/strategies/addComment", param, function (data) {
                vue.queryStatisVo(param.strategyId);
                vue.commentPage(1, param.strategyId);
            })
        },
        queryStatisVo: function (sid) {
            //统计数据
            ajaxGet("/strategies/statisVo", {sid: sid}, function (data) {
                vue.vo = data.data;
            })
        },
        queryUserFavor: function (sid, userId) {
            ajaxGet("/users/strategies/favor", {sid: sid, userId: userId}, function (data) {
                vue.sids = data.data;
            })
        },

        //微博分享
        //按钮的点击事件
        sina: function (sid) {
            $('.sina').click(function () {
                //标题
                window.sharetitle = $(this).data('title');
                //链接
                window.shareUrl = $(this).data('url');
                //图片
                window.sharePic = $(this).data('pic');
                share();
            });
            function share() {
                (function (s, d, e) {
                    try {
                    } catch (e) {
                    }
                    var f = 'http://service.weibo.com/share/share.php?', u = d.location.href,
                        p = ['url=', e(u), '&title=', e(window.sharetitle)
                            , '&appkey=82015185', '&pic='
                            , e(window.sharePic)].join('');

                    function a() {
                        if (!window.open([f, p].join(''), 'mb'
                            , ['toolbar=0,status=0,resizable=1,width=620,height=450,left=', (s.width - 620) / 2
                                , ',top=', (s.height - 450) / 2].join(''))) u.href = [f, p].join('');
                    };
                    if (/Firefox/.test(navigator.userAgent)) {
                        setTimeout(a, 0)
                    } else {
                        a()
                    }
                })(screen, document, encodeURIComponent);
            }
            //后台增加一个分享数 点击了就是分享了 后面的确定接收微博分享成功后的信息暂时做不出来
           /* ajaxPost("/strategies/weibo", {sid: sid}, function (data) {
                if (data.data) {
                    popup("成功分享");
                }
            })*/
        }
    },
    filters: {
        dateFormat: function (date) {
            return dateFormat(date, "YYYY-MM-DD HH:mm:ss")
        }
    },
    mounted: function () {
        var param = getParams();

        //攻略明细
        ajaxGet("/strategies/detail", {id: param.id}, function (data) {
            vue.strategy = data.data;
        })

        //统计数据
        this.queryStatisVo(param.id);

        //用户收藏攻略id集合
        if (user) {
            this.queryUserFavor(param.id, user.id);
        }

        //攻略评论分页
        this.commentPage(1, param.id);
    }
});

